// i.d(t, { $: () => PinHeadMesh })
import * as THREE from "three"
import o from "../math/49487"
import renderOrderEnum from "../enum/renderOrder.enum"
import funcUtil from "../utils/func.util"
import configConst from "../constant/config.const"
import CanvasData from "../data/CanvasData"
import { matterItemData } from "../module/PinsModule"
import { RenderLayer } from "../utils/RenderLayers"
import { PinHeadMaterial } from "../material/PinMaterial"
export default class PinHeadMesh extends THREE.Mesh<THREE.PlaneGeometry, PinHeadMaterial> {
  geomScale: THREE.Vector3
  worldPosition: THREE.Vector3
  cameraPosition: THREE.Vector3
  constructor(e, t: THREE.PlaneGeometry, i: PinHeadMaterial, n: RenderLayer) {
    super(t, i)
    this.geomScale = new THREE.Vector3(1, 1, 1)
    this.userData.sid = e
    this.worldPosition = new THREE.Vector3()
    this.cameraPosition = new THREE.Vector3()
    this.layers.mask = n.mask
    this.renderOrder = renderOrderEnum.pins
    this.scale.set(0.01, 0.01, 0.01)
  }
  dispose() {
    this.material.dispose()
    this.geometry.dispose()
  }
  updatePosition(e: matterItemData) {
    this.position.copy(e.stemNormal).setLength(Math.max(e.stemLength, 0.01))
  }
  update(e: THREE.PerspectiveCamera, t: number, i: CanvasData) {
    const s = configConst.PlaneBufferGeometryConfg.pinHeadMesh.scale
    this.getWorldPosition(this.worldPosition)
    const c = e.getWorldPosition(this.cameraPosition).distanceTo(this.worldPosition)
    this.quaternion.copy(e.quaternion)
    const h = s.maxSize - (s.maxSize - s.minSize) * o.C(c, s.nearBound, s.farBound)
    const d = this.worldPosition.clone().project(e)
    const u = new THREE.Vector3(i.width / 2, i.height / 2, 1).multiply(d)
    const p = new THREE.Vector3(h / 2, 0, 0).add(u)
    const m =
      new THREE.Vector3(2 / i.width, 2 / i.height, 1).multiply(p).clone().unproject(e).clone().distanceTo(this.worldPosition) *
      (1 + (s.responsiveness / 100) * (t - 1))
    const g = funcUtil.getMax(m, 0.00001, m)
    this.scale.set(g * this.geomScale.x, g * this.geomScale.y, g * this.geomScale.z)
  }
}
